package com.bstek.bdf2.protal.manageProtal;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import com.bstek.bdf2.core.context.ContextHolder;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.utils.AppUtils;


/**
 *
 * @author zxq
 */
@Repository(ManageProtalDao.BEAN_ID)
public class ManageProtalDao extends CoreHibernateDao {
	public static final String BEAN_ID="mes.ManageProtalDao";

	/**
	 * 
	 * @param page
	 * @param parameter
	 */
	public void queryManageProtal(Page<ManageProtal> page, Map<String, Object> parameter) {
		DetachedCriteria dc = DetachedCriteria.forClass(ManageProtal.class);
		if(parameter!=null) {
			String issueState = (String) parameter.getOrDefault("issueState", null);
			if(!StringUtils.isEmpty(issueState)) {
				dc.add(Restrictions.eq("issueState", issueState));
			}
			String moduleProtal = (String) parameter.getOrDefault("moduleProtal", null);
			if(!StringUtils.isEmpty(moduleProtal)) {
				dc.add(Restrictions.eq("moduleProtal", moduleProtal));
			}
			 Date beginDate = (Date) parameter.getOrDefault("beginDate", null);
			 Date endDate = (Date) parameter.getOrDefault("endDate", null);
 
//			 if(beginDate!=null&&endDate!=null) {
//				 dc.add(Restrictions.sqlRestriction("{alias}.create_time between ? and ?",
//						 new Object[] {
//								 getLocalDateStr(beginDate, "yyyy-MM-dd"),
//								 getLocalDateStr(endDate, "yyyy-MM-dd")},
//				 StringType.INSTANCE));
//			 }
			 if(beginDate!=null) {
				 dc.add(Restrictions.sqlRestriction("{alias}.create_time>=?",getLocalDateStr(beginDate, "yyyy-MM-dd"),StringType.INSTANCE));
 			 }
			 if(endDate!=null) {
 				 dc.add(Restrictions.sqlRestriction("{alias}.create_time<=?",getLocalDateStr(endDate, "yyyy-MM-dd"),StringType.INSTANCE));
 			 }
		}
		 dc.addOrder(Order.desc("createTime"));
		this.pagingQuery(page, dc);
	}

	public static void main(String[] args) {
		String str = getLocalDateStr(new Date(), "yyyy-MM-dd");
		System.err.println(str);
	}
	
	public static String getLocalDateStr(Date date,String formatter) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);     
        String dateStr = dateTimeFormatter.format(localDateTime);     
	     return dateStr;
	 }

	/**
	 *
	 * @param coll
	 */
	public void saveManageProtal(Collection<ManageProtal> coll) {
		Session session=this.getSession();
		for(ManageProtal emp:coll){
			EntityState state=EntityUtils.getState(emp);
			if(state.equals(EntityState.NEW)){
				emp.setModuleProtal("角色2");
				emp.setId(AppUtils.generateUUID());
				emp.setCreateTime(new Date());
				emp.setCreateUser(ContextHolder.getLoginUserName());
				session.save(emp);
			}else if(state.equals(EntityState.MODIFIED)){
				session.update(emp);
			}else if(state.equals(EntityState.DELETED)){
				session.delete(emp);
			}else{
				
			}
		}
		
	}
 	@Transactional(rollbackFor = Exception.class)
	public void save(ManageProtal a){
		this.getSession().save(a);
	}
}
